import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { UrlService } from '../common/url.service';
import { res } from '../common/Interface';
import { NzMessageService } from 'ng-zorro-antd/message';
import JSEncrypt from 'jsencrypt';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less'],
})
export class LoginComponent implements OnInit {
  constructor(
    private router: Router,
    private http: HttpClient,
    private url: UrlService,
    private message: NzMessageService
  ) {}
  passwordVisible = false;
  user = {
    username: '',
    password: '',
  };
  ngOnInit(): void {}
  login() {
    const jsencrypt = new JSEncrypt();
    var pubKey =
      '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfXLTp2wFd6/AAQV7ZVcavRndcgyi9ec1cVNb4NWrXtY7R+QBxeFiCbRyGgVGPzx+Hzxe/BIObVzaAkvNHZz6fGHR336RoaZ18ZE7Pim9SwPWM5KNx+2q11llK5zkTQarQq0y2sb/dx+ZU7DcT2z3+DtqjQU+QytFKYySde+Hu4QIDAQAB-----END PUBLIC KEY-----';
    jsencrypt.setPublicKey(pubKey); //设置公钥
    const password = jsencrypt.encrypt(this.user.password);
    this.http
      .post(`${this.url.baseurl}/user/login`, { ...this.user, password })
      .subscribe((res: res) => {
        const { code } = res;
        if (code == 200) {
          this.router.navigateByUrl('/main');
          localStorage.setItem('token', res.data['token']);
        } else {
          this.message.error(res.msg);
        }
      });
  }
}
